package com.bafa.ajax;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bafa.exceptions.ErrorException;
import com.bafa.objects.Match;
import com.bafa.objects.Scorer;
import com.bafa.services.RetrieveServices;

public class DisplayScorersAjax extends HttpServlet {

	
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
	
		Match match = RetrieveServices.getMatch(new Integer(request.getParameter("MatchId")).intValue());
		StringBuffer xml = new StringBuffer();
		xml.append("<scorers>");
		if(request.getParameter("TeamId").equalsIgnoreCase("A")){
			for(int i=0;i<match.scorersA().length;i++){
				String scorer = match.scorersA()[i];
				String goals = ""+match.getScorersA().get(match.scorersA()[i]);
				xml.append("<scorer>");
				xml.append("<name>"+scorer+"</name>");
				xml.append("<goals>"+goals+"</goals>");
				xml.append("</scorer>");
			}
		}else{
			for(int i=0;i<match.scorersB().length;i++){
				String scorer = match.scorersB()[i];
				String goals = ""+match.getScorersB().get(match.scorersB()[i]);
				xml.append("<scorer>");
				xml.append("<name>"+scorer+"</name>");
				xml.append("<goals>"+goals+"</goals>");
				xml.append("</scorer>");
			}
		}
		xml.append("</scorers>");
		
		response.setContentType("text/xml");
	    response.getWriter().write(xml.toString());
	}
}
